/**
 * Title: LoginManager.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2014-2-28 上午9:51:31
 * Version 1.0
 */
package com.bchun.gamepk.web.model.manager;

import com.bchun.gamepk.model.UsersInfo;
import com.bchun.gamepk.service.IUsersInfoService;
import com.bchun.gamepk.web.common.ResultCode;
import com.bchun.gamepk.web.common.ResultMessage;
import com.bchun.gamepk.web.model.request.LoginRequest;
import com.bchun.gamepk.web.model.request.RegRequest;
import com.bchun.gamepk.web.model.resposne.LoginResponse;
import com.bchun.gamepk.web.model.resposne.ServerResult;
import com.bchun.gamepk.web.model.resposne.UserRegResponse;
import com.bchun.gamepk.web.model.vo.AccountInfo;
import com.bchun.gamepk.web.model.vo.UserInfo;
import com.google.gson.Gson;

/** 
 * ClassName: LoginManager
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2014-2-28 上午9:51:31
 */
public class LoginManager {
	public LoginResponse login(String requestStr, IUsersInfoService usersInfoService) {
		LoginResponse response = new LoginResponse();
		ServerResult serverResult = new ServerResult();
		Gson gson = new Gson();
		LoginRequest request = null;
		try {
			request = gson.fromJson(requestStr, LoginRequest.class);
		} catch (Exception e) {
			serverResult.setResultCode(ResultCode.fail.getValue());
			serverResult.setResultMessage(ResultMessage.invalidJson.getValue());
			response.setServerResult(serverResult);
			return response;
		}
		
		UsersInfo usersInfo = usersInfoService.getByLoginNameAndPwd(request.getLoginName(),request.getPassword());
		
		if (usersInfo == null)
		{
			serverResult.setResultCode(ResultCode.fail.getValue());
			serverResult.setResultMessage(ResultMessage.accountOrPwdError.getValue());
			response.setServerResult(serverResult);
			return response;
		}
		
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(usersInfo.getLoginName());
		userInfo.setUserName(usersInfo.getUserName());
		userInfo.setEmail(usersInfo.getEmail());
		userInfo.setMobileNumber(usersInfo.getPhoneNumber());
		userInfo.setRecipientAddress(usersInfo.getAddress());
		
		AccountInfo accountInfo = new AccountInfo();
		accountInfo.setGoldNum(0);
		accountInfo.setLevel(1);
		accountInfo.setLevelName("1级");
		
		serverResult.setResultCode(ResultCode.success.getValue());
		serverResult.setResultMessage(ResultMessage.suc.getValue());
		
		response.setUserInfo(userInfo);
		response.setAccountInfo(accountInfo);
		response.setServerResult(serverResult);
		return response;
	}
}
